Skip to content

Commit 85294e1

Browse files
refactor(utils): move output-target related utils to @utils (#4225)
This moves a bunch of utility functions (and some constants) from src/compiler/output-targets/output-utils.ts to instead live in a new file in the `@utils` directory (`src/utils`). This is done to reflect the fact that functions in the file are used outside of the `src/compiler/` bundle so they should be pulled out into a common location.
1 parent 2a5ee1d commit 85294e1

File tree

69 files changed

+278
-176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+278
-176
lines changed

scripts/bundles/internal-platform-hydrate.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ export async function internalHydrate(opts: BuildOptions) {
3939
banner: getBanner(opts, 'Stencil Hydrate Platform'),
4040
preferConst: true,
4141
},
42+
/**
43+
* The `internal-platform-hydrate` bundle depends on functions which use
44+
* the output-target utils which, in turn, imports `path`. However, none of
45+
* the functions which it uses actually _use_ functioned exported by `path`,
46+
* so when bundling it doesn't actually import anything from `path` but if
47+
* you don't set this option Rollup will leave an unqualified import like
48+
* so:
49+
*
50+
* ```ts
51+
* import "path";
52+
* ```
53+
*
54+
* If you say `moduleSideEffects: false` you're basically saying 'dont assume
55+
* that imported a module has a desired side effect' which allows an import that
56+
* doesn't result in any symbols being pulled into the importing module to be
57+
* eliminated.
58+
*
59+
* Another way to say this is that without this change the treeshaking
60+
* validation for `test.dist` doesn't pass for this bundle.
61+
*/
62+
treeshake: {
63+
moduleSideEffects: false,
64+
},
4265
plugins: [
4366
{
4467
name: 'internalHydratePlugin',
@@ -66,6 +89,17 @@ export async function internalHydrate(opts: BuildOptions) {
6689
banner: getBanner(opts, 'Stencil Hydrate Runner'),
6790
preferConst: true,
6891
},
92+
/**
93+
* See above for an explanation, this is necessary to eliminate an
94+
* unqualified import of `"path"`, like so:
95+
*
96+
* ```ts
97+
* import "path";
98+
* ```
99+
*/
100+
treeshake: {
101+
moduleSideEffects: false,
102+
},
69103
plugins: [
70104
aliasPlugin(opts),
71105
replacePlugin(opts),

src/cli/task-docs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { isOutputTargetDocs } from '../compiler/output-targets/output-utils';
1+
import { isOutputTargetDocs } from '@utils';
2+
23
import type { ValidatedConfig } from '../declarations';
34
import type { CoreCompiler } from './load-compiler';
45
import { startupCompilerLog } from './logs';

src/cli/telemetry/telemetry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { isOutputTargetHydrate, WWW } from '../../compiler/output-targets/output-utils';
1+
import { isOutputTargetHydrate, WWW } from '@utils';
2+
23
import { IS_BROWSER_ENV } from '../../compiler/sys/environment';
34
import type * as d from '../../declarations';
45
import { readConfig, updateConfig, writeConfig } from '../ionic-config';

src/cli/telemetry/test/telemetry.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as coreCompiler from '@stencil/core/compiler';
22
import { mockValidatedConfig } from '@stencil/core/testing';
3+
import { DIST, DIST_CUSTOM_ELEMENTS, DIST_HYDRATE_SCRIPT, WWW } from '@utils';
34

45
import { createConfigFlags } from '../../../cli/config-flags';
5-
import { DIST, DIST_CUSTOM_ELEMENTS, DIST_HYDRATE_SCRIPT, WWW } from '../../../compiler/output-targets/output-utils';
66
import * as environment from '../../../compiler/sys/environment';
77
import { createSystem } from '../../../compiler/sys/stencil-sys';
88
import type * as d from '../../../declarations';

src/compiler/build/build-hmr.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { isGlob, normalizePath, sortBy } from '@utils';
1+
import { isGlob, isOutputTargetWww, normalizePath, sortBy } from '@utils';
22
import minimatch from 'minimatch';
33
import { basename } from 'path';
44

55
import type * as d from '../../declarations';
6-
import { isOutputTargetWww } from '../output-targets/output-utils';
76
import { getScopeId } from '../style/scope-css';
87

98
export const generateHmr = (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx) => {

src/compiler/build/build-stats.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { byteSize, sortBy } from '@utils';
1+
import { byteSize, isOutputTargetStats, sortBy } from '@utils';
22

33
import type * as d from '../../declarations';
4-
import { isOutputTargetStats } from '../output-targets/output-utils';
54

65
/**
76
* Generates the Build Stats from the buildCtx. Writes any files to the file system.

src/compiler/bundle/ext-transforms-plugin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { hasError, normalizeFsPath } from '@utils';
1+
import { hasError, isOutputTargetDistCollection, normalizeFsPath } from '@utils';
22
import { join, relative } from 'path';
33
import type { Plugin } from 'rollup';
44

55
import type * as d from '../../declarations';
6-
import { isOutputTargetDistCollection } from '../output-targets/output-utils';
76
import { runPluginTransformsEsmImports } from '../plugin/plugin';
87
import { parseImportPath } from '../transformers/stencil-import-path';
98
import type { BundleOptions } from './bundle-interface';

src/compiler/bundle/server-plugin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { isString, normalizeFsPath } from '@utils';
1+
import { isOutputTargetHydrate, isString, normalizeFsPath } from '@utils';
22
import { isAbsolute } from 'path';
33
import type { Plugin } from 'rollup';
44

55
import type * as d from '../../declarations';
6-
import { isOutputTargetHydrate } from '../output-targets/output-utils';
76

87
export const serverPlugin = (config: d.ValidatedConfig, platform: string): Plugin => {
98
const isHydrateBundle = platform === 'hydrate';

src/compiler/config/outputs/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { buildError } from '@utils';
1+
import { buildError, isValidConfigOutputTarget, VALID_CONFIG_OUTPUT_TARGETS } from '@utils';
22

33
import type * as d from '../../../declarations';
4-
import { isValidConfigOutputTarget, VALID_CONFIG_OUTPUT_TARGETS } from '../../output-targets/output-utils';
54
import { validateCollection } from './validate-collection';
65
import { validateCustomElement } from './validate-custom-element';
76
import { validateCustomOutput } from './validate-custom-output';

src/compiler/config/outputs/validate-collection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { isOutputTargetDistCollection } from '@utils';
2+
13
import type * as d from '../../../declarations';
2-
import { isOutputTargetDistCollection } from '../../output-targets/output-utils';
34
import { getAbsolutePath } from '../config-utils';
45

56
/**

0 commit comments

Comments
 (0)