serverless-plugin-typescript icon indicating copy to clipboard operation
serverless-plugin-typescript copied to clipboard

Changes to includes not copied

Open gabelerner opened this issue 5 years ago • 6 comments

Since this plugin takes the responsibility of copying the package.include to the .build folder, when running with serverless offline, I noticed that my package.include files are not updated in the build when changed.

The reason for this seems to be: https://github.com/prisma/serverless-plugin-typescript/blob/d6496abb510984f80c07de417a3134c806068830/src/index.ts#L148

A quick fix would be to continue to create directories when they don't exist but ALWAYS copy the package.include files.

An extra feature would be to also add a watch for the package.include files so that they are copied when they change.

Right now, I'm forced to ctrl+c out of serverless offline, delete my .build folder, and restart serverless offline to see the new changes.

gabelerner avatar May 28 '19 19:05 gabelerner

This still seems to be a problem with this package.

https://github.com/prisma/serverless-plugin-typescript/blob/424fe4906bd15ceeeda6139dfe9e0f8bf873c4c3/src/index.ts#L176-L178

Happy to work on a PR if you'll accept it.

First step would be to remove the file existence guard so that the plugin will always copy the included files (that will prevent stale ones staying there, even after they've been updated).

That could be further improved with another watcher that watches the glob from package.include and simply calls copyExtras whenever it changes.

Alternatively, the extras could be symlinked to the .build directory in the same way node_modules and package.json are now, to remove the need for a watcher.

danprince avatar Sep 03 '19 23:09 danprince

As a concrete example of why this is problematic, consider a standard GraphQL workflow.

We have a schema.graphql file that is read from one of our handlers and therefore needs to be copied into the .build directory. We're currently doing it through package.include.

  • Edit schema
  • Kill the current serverless offline process
  • Remove the .build directory
  • Run serverless offline start
  • Wait for the serverless offline process to restart
  • Edit resolvers

Usually that flow would be:

  • Edit schema
  • Edit resolvers

danprince avatar Sep 03 '19 23:09 danprince

I'm also having this problem. @danprince do you have any workarounds for this issue?

lucas-barros avatar Sep 25 '19 11:09 lucas-barros

@lucas-barros Switch to serverless-webpack and use ts-loader then you get better control of the output structure, at the cost of doing more configuration up front and making webpack part of your toolchain.

danprince avatar Sep 25 '19 14:09 danprince

@danprince Yeah I realized that as well, thanks anyway!

lucas-barros avatar Sep 25 '19 14:09 lucas-barros

So serverless-webpack worked well, here is my webpack file. I used copy-webpack-plugin to move the assets to the build folder.

const slsw = require('serverless-webpack')
const nodeExternals = require('webpack-node-externals')
const CopyPlugin = require('copy-webpack-plugin')
const DefinePlugin = require('webpack').DefinePlugin
const isLocal = slsw.lib.webpack.isLocal

module.exports = {
  mode: isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  externals: [nodeExternals()],
  devtool: 'source-map',
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.ts', '.tsx']
  },
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js'
  },
  target: 'node',
  module: {
    rules: [{ test: /\.ts$/, exclude: /node_modules/, loader: 'ts-loader' }]
  },
  plugins: [
    new CopyPlugin([
      { from: 'templates/**/*', to: '', context: 'src/' },
      { from: 'assets/**/*', to: '', context: 'src/' }
    ]),
    new DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
      })
  ]
}

lucas-barros avatar Sep 25 '19 19:09 lucas-barros