Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module ''@app/env" only at first run #32

Open
pmargreff opened this issue Jul 2, 2018 · 0 comments
Open

Cannot find module ''@app/env" only at first run #32

pmargreff opened this issue Jul 2, 2018 · 0 comments

Comments

@pmargreff
Copy link

The import only works after a recompile, ever first compilation gives an import error, but if I retry with any change on file (new line, on this example) it works fine. I have no guess why

Here is a compilation log:

$ MY_ENV=dev ionic-app-scripts serve
[19:45:14]  ionic-app-scripts 3.1.10 
[19:45:14]  watch started ... 
[19:45:14]  build dev started ... 
[19:45:14]  clean started ... 
[19:45:14]  clean finished in 5 ms 
[19:45:14]  copy started ... 
[19:45:14]  deeplinks started ... 
[19:45:14]  deeplinks finished in 34 ms 
[19:45:14]  transpile started ... 
[19:45:17]  typescript: src/providers/authentication/authentication.ts, line: 5 
            Cannot find module '@app/env'. 

       L4:  import 'rxjs/add/operator/map';
       L5:  import { API } from '@app/env';

[19:45:17]  dev server running: http://localhost:8100/ 

[19:45:17]  copy finished in 2.65 s 
[19:45:17]  watch ready in 2.69 s 
[19:45:22]  build started ... 
[19:45:22]  deeplinks update started ... 
[19:45:22]  deeplinks update finished in 24 ms 
[19:45:22]  transpile update started ... 
[19:45:22]  transpile update finished in 41 ms 
[19:45:22]  webpack started ... 
[19:45:34]  webpack finished in 12.61 s 
[19:45:34]  sass started ... 
Without `from` option PostCSS could generate wrong source map and will not find Browserslist config. Set it to CSS file path or to `undefined` to prevent this warning.
[19:45:35]  sass finished in 819 ms 
[19:45:35]  build finished in 13.56 s 

Here is my tsconfig file:

{
  "compilerOptions": {
    "typeRoots": ["node_modules/@types"],
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "src/**/*.spec.ts",
    "src/**/__tests__/*.ts"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  },
  "baseUrl": "./src",
  "paths": {
    "@app/env": [
      "environments/dev.ts", "environments/test.ts", "environments/prod.ts"
    ]
  }
}

And here the webpack file:

/*
 * The webpack config exports an object that has a valid webpack configuration
 * For each environment name. By default, there are two Ionic environments:
 * "dev" and "prod". As such, the webpack.config.js exports a dictionary object
 * with "keys" for "dev" and "prod", where the value is a valid webpack configuration
 * For details on configuring webpack, see their documentation here
 * https://webpack.js.org/configuration/
 */

var webpack = require('webpack');
var chalk = require("chalk");
var fs = require('fs');
var path = require('path');

var ionicWebpackFactory = require(process.env.IONIC_WEBPACK_FACTORY);
var ModuleConcatPlugin = require('webpack/lib/optimize/ModuleConcatenationPlugin');
var PurifyPlugin = require('@angular-devkit/build-optimizer').PurifyPlugin;

var useDefaultConfig = require('@ionic/app-scripts/config/webpack.config.js');

var env = process.env.MY_ENV;

useDefaultConfig.prod.resolve.alias = {
  "@app/env": path.resolve(environmentPath('prod'))
};

useDefaultConfig.dev.resolve.alias = {
  "@app/env": path.resolve(environmentPath('dev'))
};

useDefaultConfig.dev.plugins = [
  ionicWebpackFactory.getIonicEnvironmentPlugin(),
  ionicWebpackFactory.getCommonChunksPlugin(),
  new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
    result.request = result.request.replace(/typeorm/, "typeorm/browser");
  }),
  new webpack.ProvidePlugin({
    'window.SQL': 'sql.js/js/sql.js'
  })
]

useDefaultConfig.prod.plugins = [
  ionicWebpackFactory.getIonicEnvironmentPlugin(),
  ionicWebpackFactory.getCommonChunksPlugin(),
  new ModuleConcatPlugin(),
  new PurifyPlugin(),
  new webpack.NormalModuleReplacementPlugin(/typeorm$/, function (result) {
    result.request = result.request.replace(/typeorm/, "typeorm/browser");
  })
]

function environmentPath(env) {
  var filePath = './src/environments/'+ env + '.ts';
  if (!fs.existsSync(filePath)) {
    console.log(chalk.red('\n' + filePath + ' does not exist!'));
  } else {
    return filePath;
  }
}

module.exports = function () {
  return useDefaultConfig;
};

I had some changes to run with typeorm and to find the file on a simpler path (environments/dev.ts ...).

But I don't see how they have to lead to this bug, anyone can help me to identify the problem here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant