Skip to content

Commit

Permalink
fix(js): default build should work when rollup is selected (#14971)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham authored Feb 22, 2023
1 parent 828eff7 commit eb977bf
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 56 deletions.
2 changes: 1 addition & 1 deletion docs/generated/packages/js/generators/library.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"bundler": {
"description": "The bundler to use.",
"type": "string",
"enum": ["none", "esbuild", "rollup", "vite", "webpack"],
"enum": ["none", "esbuild", "rollup", "vite"],
"default": "none",
"x-priority": "important"
},
Expand Down
31 changes: 0 additions & 31 deletions e2e/webpack/src/webpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,4 @@ describe('Webpack Plugin', () => {
output = runCommand(`node dist/libs/${myPkg}/main.js`);
expect(output).toMatch(/Hello/);
}, 500000);

it('should define process.env variables only for --platform=web', async () => {
const myPkg = uniq('my-pkg');
runCLI(`generate @nrwl/js:lib ${myPkg} --bundler=webpack`);
updateFile(
`libs/${myPkg}/src/index.ts`,
`console.log(process.env['NX_TEST_VAR']);\n`
);

runCLI(`build ${myPkg} --platform=node`, {
env: {
NX_TEST_VAR: 'Hello build time',
},
});

expect(
runCommand(`node dist/libs/${myPkg}/main.js`, {
env: {
NX_TEST_VAR: 'Hello run time',
},
})
).toMatch(/Hello run time/);

runCLI(`build ${myPkg} --platform=web`, {
env: {
NX_TEST_VAR: 'Hello build time',
},
});

expect(readFile(`dist/libs/${myPkg}/main.js`)).toMatch(/Hello build time/);
}, 300_000);
});
23 changes: 3 additions & 20 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ export async function projectGenerator(
});
tasks.push(viteTask);
}

if (schema.bundler === 'webpack' || schema.bundler === 'rollup') {
if (schema.bundler === 'rollup') {
ensureBabelRootConfigExists(tree);
}

Expand Down Expand Up @@ -154,18 +153,12 @@ function addProject(
outputPath,
main: `${options.projectRoot}/src/index` + (options.js ? '.js' : '.ts'),
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
// TODO(jack): assets for webpack and rollup have validation that we need to fix (assets must be under <project-root>/src)
// TODO(jack): assets for rollup have validation that we need to fix (assets must be under <project-root>/src)
assets:
options.bundler === 'webpack' || options.bundler === 'rollup'
? []
: [`${options.projectRoot}/*.md`],
options.bundler === 'rollup' ? [] : [`${options.projectRoot}/*.md`],
},
};

if (options.bundler === 'webpack') {
projectConfiguration.targets.build.options.babelUpwardRootMode = true;
}

if (options.compiler === 'swc' && options.skipTypeCheck) {
projectConfiguration.targets.build.options.skipTypeCheck = true;
}
Expand Down Expand Up @@ -446,14 +439,6 @@ function addProjectDependencies(
);
}

if (options.bundler == 'webpack') {
return addDependenciesToPackageJson(
tree,
{},
{ '@nrwl/webpack': nxVersion, '@types/node': typesNodeVersion }
);
}

// noop
return () => {};
}
Expand All @@ -464,8 +449,6 @@ function getBuildExecutor(options: NormalizedSchema) {
return `@nrwl/esbuild:esbuild`;
case 'rollup':
return `@nrwl/rollup:rollup`;
case 'webpack':
return `@nrwl/webpack:webpack`;
default:
return `@nrwl/js:${options.compiler}`;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/generators/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"bundler": {
"description": "The bundler to use.",
"type": "string",
"enum": ["none", "esbuild", "rollup", "vite", "webpack"],
"enum": ["none", "esbuild", "rollup", "vite"],
"default": "none",
"x-priority": "important"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import { TransformerEntry } from './typescript/types';

export type Compiler = 'tsc' | 'swc';
export type Bundler = 'none' | 'rollup' | 'esbuild' | 'vite' | 'webpack';
export type Bundler = 'none' | 'rollup' | 'esbuild' | 'vite';

export interface LibraryGeneratorSchema {
name: string;
Expand Down
6 changes: 4 additions & 2 deletions packages/rollup/src/executors/rollup/lib/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename, dirname, relative, resolve } from 'path';
import { basename, dirname, join, relative, resolve } from 'path';
import { statSync } from 'fs';
import { normalizePath } from '@nrwl/devkit';

Expand All @@ -18,7 +18,9 @@ export function normalizeRollupExecutorOptions(
): NormalizedRollupExecutorOptions {
const main = `${root}/${options.main}`;
const entryRoot = dirname(main);
const project = `${root}/${options.project}`;
const project = options.project
? `${root}/${options.project}`
: join(root, 'package.json');
const projectRoot = dirname(project);
const outputPath = `${root}/${options.outputPath}`;

Expand Down

1 comment on commit eb977bf

@vercel
Copy link

@vercel vercel bot commented on eb977bf Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.